<template>
	<view>
		<simple-toolbar :title="$t('setting.title')">
			<view class="returnToConfirm">
				<el-button @click="confirm" type="primary" :disabled="success">{{ $t('global.action.confirm') }}
				</el-button>
			</view>
		</simple-toolbar>

		<view class="pad-base form">
			<el-form :model="formData" :rules="rules" ref="formData" label-position="top">
				<el-form-item :label="$t('setting.tib')" prop="netspace">
					<el-input v-model="formData.netspace" type="number" :placeholder="$t('setting.tib_pla')">
						<template slot="suffix">TiB</template>
					</el-input>
				</el-form-item>
				<el-form-item :label="$t('setting.rewards')" prop="current_earning">
					<el-input v-model="formData.current_earning" type="number" :placeholder="$t('setting.rewards_pla')">
						<template slot="suffix">XCH</template>
					</el-input>
				</el-form-item>
				<el-form-item :label="$t('setting.price')" prop="chia_price">
					<el-input v-model="formData.chia_price" type="number" :placeholder="$t('setting.price_pla')">
						<template slot="suffix">USD</template>
					</el-input>
				</el-form-item>
			</el-form>
		</view>
	</view>
</template>

<script>
	import {
		getChiainfo,
		updateChiainfo
	} from "../../../js_sdk/uni-admin/setting.js"

	export default {
		onShow() {
			this.getChiainfo()
		},
		data() {
			return {
				success: false,
				formData: {
					netspace: '',
					current_earning: '',
					chia_price: ''
				},
				rules: {
					tib: [{
						required: true,
						validator: (rule, value, callback) => {
							if (value === '') {
								callback(new Error(this.$t('setting.tib_pla')))
							} else {
								callback()
							}
						},
						trigger: 'blur'
					}],
					rewards: [{
						required: true,
						validator: (rule, value, callback) => {
							if (value === '') {
								callback(new Error(this.$t('setting.rewards_pla')))
							} else {
								callback()
							}
						},
						trigger: 'blur'
					}],
					price: [{
						required: true,
						validator: (rule, value, callback) => {
							if (value === '') {
								callback(new Error(this.$t('setting.price_pla')))
							} else {
								callback()
							}
						},
						trigger: 'blur'
					}]
				}
			}
		},
		methods: {
			async getChiainfo() {
				let res = await getChiainfo()
				this.formData = res.data
			},
			confirm() {
				this.$refs.formData.validate(async valid => {
					if (!valid) return
					let data = {
						netspace: this.formData.netspace * 1,
						current_earning: this.formData.current_earning * 1,
						chia_price: this.formData.chia_price * 1
					}
					await updateChiainfo(data)
					this.success = true
					setTimeout(() => {
						uni.navigateBack()
					}, 1000)
				});
			},
		},
	}
</script>

<style>

</style>
